package com.t174.car.controller;

import com.t174.car.dao.CarComponentsDao;
import com.t174.car.dao.CarPurchaseDao;
import com.t174.car.entity.CarComponents;
import com.t174.car.entity.CarPurchase;
import com.t174.car.entity.Page;
import com.t174.car.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/car")
@Scope("prototype")
public class CarsController {
    @Autowired
    CarComponentsDao carComponentsDao;
    @Autowired
    CarPurchaseDao carPurchaseDao;
    @Autowired
    CarService carService;

    @RequestMapping("/query")
    @ResponseBody
    public Map<String,Object> getCarInfo(CarComponents carComponents,@RequestParam(value = "offset",defaultValue="0",required = false) int offset, @RequestParam(value = "limit",defaultValue = "3",required = false)int limit){

        Map<String,Object> map = new HashMap<String,Object>();
        System.out.println(carComponents.getBrand());
        System.out.println(carComponents.getCarConfig().getColor());
        Page page = new Page();
        page.setPageNo(offset);
        page.setPageSize(limit);
        System.out.println(offset+"--"+limit);
        System.out.println((offset-1)*limit+"--"+page.getPageNo()+"--"+page.getPageSize());
        List<CarComponents> list = carComponentsDao.query(carComponents,page);
        for (CarComponents car: list) {
            System.out.println(car.getBrand()+"--"+car.getCarConfig().getColor()+"--"+car.getCarConfig().getId());
        }
        int total = carComponentsDao.getTotalCount(carComponents);
        map.put("rows",list);
        map.put("total",total);
        return map;
    }
    @RequestMapping("/seve")
    @ResponseBody
    public Integer addCar(CarPurchase carPurchase, BigDecimal money){
        System.out.println("添加汽车");
        System.out.println(carPurchase.getBrand());
        return carService.addCar(carPurchase,money);
    }

}
